package com.sskj.mine.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.tifezh.kchartlib.chart.formatter.BigValueFormatter;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.box.typeevaluator.BigDecimalEvaluator;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.LockHouseBean;
import com.sskj.mine.presenter.LockWareHousePresent;
import com.sskj.mine.util.TipMineUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by QinKang on 2018/12/15.
 */

@Route(path = RConfig.MINE_LOCK_WARE_HOUSE)//锁仓
public class LockWareHouseActivity extends BaseActivity<LockWareHousePresent> {

    @BindView(R2.id.et_lock_ware_time)
    TextView etLockWareTime;
    @BindView(R2.id.tv_lock_ware_canuse)
    TextView tvLockWareCanuse;
    @BindView(R2.id.et_lock_ware_precent)
    TextView etLockWarePrecent;
    @BindView(R2.id.et_lock_ware_reback)
    TextView etLockWareReback;
    @BindView(R2.id.tv_lock_ware_type)
    TextView etLockWareType;
    @BindView(R2.id.et_lock_ware_num)
    EditText etLockWareNum;
    @BindView(R2.id.et_lock_ware_password)
    EditText etLockWarePwd;
    @BindView(R2.id.btn_lock_ware_send)
    Button btnLockWareSend;
    @Autowired
    String code;
    @Autowired
    String id;
    @Autowired
    String usableFund;
    private List<LockHouseBean.ListBean> lockTypes = new ArrayList();
    private String lockId;//锁仓周期Id
    private String rate;
    private String backPname;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lock_ware_house;
    }

    @Override
    public LockWareHousePresent getPresenter() {
        return new LockWareHousePresent();
    }

    @Override
    protected void initView() {
        super.initView();
        ARouter.getInstance().inject(this);
        setTitle(App.INSTANCE.getString(R.string.mine_recy_item_assets75));
        mPresenter.getLockHouseType(id);
        etLockWareType.setText(code);
        etLockWareNum.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});
        tvLockWareCanuse.setText(App.INSTANCE.getString(R.string.mineWithdrawActivity13, usableFund, code));
        ClickUtil.click(etLockWareTime, () -> {
            if (lockTypes.size() > 0) {
                TipMineUtil.getLockDataList(this, lockTypes, new TipMineUtil.OnTipLockSure() {
                    @Override
                    public void onSure(LockHouseBean.ListBean data) {
                        lockId = data.getId();
                        etLockWareTime.setTextColor(getResources().getColor(R.color.commonTextEEE));
                        etLockWareTime.setText(data.getLockday() + "天");
                        rate = data.getRate();
                        backPname = data.getBack_pname();
                        etLockWarePrecent.setText(data.getRate() + "%");
                        etLockWareReback.setText(TextUtils.isEmpty(etLockWareNum.getText()) ? ""
                                : NumberUtil.keep4(new BigDecimal(etLockWareNum.getText().toString()).multiply(new BigDecimal(data.getRate())).divide(new BigDecimal("100")).toString()) + data.getBack_pname());
//                                : NumberUtil.keep4(Float.parseFloat(etLockWareNum.getText().toString()) / 100 * Float.parseFloat(data.getRate())) + data.getBack_pname());
                    }
                });
            }
        });

        RxTextView.textChanges(etLockWareNum)
                .debounce(100, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .map(CharSequence::toString)
                .subscribe(s -> {
                    if (!TextUtils.isEmpty(rate)) {
                        if (TextUtils.isEmpty(s)) {
                            s = "0";
                        }
                        etLockWareReback.setText(NumberUtil.keep4(Float.parseFloat(s) / 100 * Float.parseFloat(rate)) + backPname);
                    }
                });

        ClickUtil.click(btnLockWareSend, () -> {
            if (TextUtils.isEmpty(etLockWareNum.getText()) || etLockWareNum.getText().toString().equals("0")) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_activity_lock_ware11));
                return;
            }
            if (Float.parseFloat(etLockWareNum.getText().toString()) > Float.parseFloat(usableFund)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_activity_lock_ware44));
                return;
            }
            if (TextUtils.isEmpty(etLockWarePwd.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity1));
                return;
            }
            if (etLockWareTime.getText().equals(App.INSTANCE.getString(R.string.mine_activity_lock_ware22)) || TextUtils.isEmpty(lockId)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_activity_lock_ware22));
                return;
            }
            if (!CheckUtil.isPayPwd(etLockWarePwd.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity4));
                return;
            }
            mPresenter.commitLockHouse(lockId, etLockWareNum.getText().toString(), etLockWarePwd.getText().toString());
        });
    }

    public void setLockTypies(LockHouseBean lockTypies) {
        lockTypes.addAll(lockTypies.getList());
    }

}
